<template>
  <view>
    <view class="info" @tap="tapInfo">
      <view class="avatar">
        <image :src="userInfo.avatar!==null?userInfo.avatar:'../images/usericon.png'" class="img" />
        <!-- <image src="https://pica.zhimg.com/80/v2-e033acbeacba3cd43e4874b1fa34afc8_720w.jpg?source=1940ef5c" v-if="userInfo.avatar" class="img" /> -->
      </view>
      <view class="username">{{ userInfo.nickname }}</view>
    </view>
    <view class="profit-info">
      <view class="order-num">
        接单数
        <view style="color: #fb841e">0 单</view>
      </view>
      <view class="profit">
        总收益
        <view style="color: #fb841e">0.00 元</view>
      </view>
    </view>
    <scroll-view style="height: 600px" :scroll-y="true">
      <view v-if="orderList.length">
        <view v-for="index in orderList.length" :key="index">
          <order :orderItem="orderList[0]"></order>
        </view>
      </view>
    </scroll-view>
    <view class="taking-order" @tap="changeState">
      {{ isWorking ? "停止" : "接单" }}
    </view>
    <view class="taking0order child1"></view>
    <view class="taking0order child2"></view>
  </view>
</template>
<script>
import "./index.less";
import order from "./components/order.vue";
import Taro from "@tarojs/taro";
import {
  set as setGlobalData,
  get as getGlobalData,
} from "../../utils/global_data";
import { baseUrl } from "../../utils/baseurl";
export default {
  components: {
    order,
  },
  data() {
    return {
      isWorking: false,
      latitude: 23.099994,
      longitude: 113.32452,
      driverId: 0,
      timeId: "",
      orderList: [],
      userInfo: {
        avatar: "../images/usericon.png",
        nickname: "用户名",
      },
    };
  },
  onLoad() {
    this.isWorking = false;
    // this.isWorking = getGlobalData("isWorking");
    this.getCurrentUser();
  },
  methods: {
    tapInfo() {
      const isLogin = getGlobalData("isLogin");
      if (!isLogin) {
        Taro.redirectTo({ url: "/pages/login/login" });
      }
    },
    changeState() {
      const isLogin = getGlobalData("isLogin");
      if (!isLogin) {
        Taro.redirectTo({ url: "/pages/login/login" });
      } else {
        this.isWorking = !this.isWorking;
        setGlobalData("isWorking", this.isWorking);
        Taro.setStorageSync("isWorking", this.isWorking);
        console.log("切换状态", getGlobalData("isWorking"));
        Taro.request({
          url:
            baseUrl.driver +
            `travel/driver/${this.isWorking ? "grab" : "stop"}`,
          method: "POST",
          data: {
            account: this.driverId,
          },
        }).then((res) => {
          console.log("切换接单状态", res.data);
        });
        if (this.isWorking) {
          clearInterval(this.timeId);
          this.timeId = setInterval(() => {
            this.getOrder();
          }, 2000);
        } else {
          clearInterval(this.timeId)
        }
      }
    },
    // 获取司机个人信息
    async getCurrentUser() {
      const token = getGlobalData("token");
      console.log("token", token);
      await Taro.request({
        url: baseUrl.driver + "travel/driver/currentDriver",
        method: "GET",
        header: {
          Authorization: token,
        },
      }).then((res) => {
        console.log(res.data);
        // this.passengerId = res.data
        // 登录成功，修改登录状态以及用户信息
        if (res.data.success) {
          setGlobalData("isLogin", true);
          setGlobalData("userInfo", res.data.data);
          this.$set(this.userInfo, "avatar", res.data.data.avatar);
          this.$set(this.userInfo, "nickname", res.data.data.nickname);
          this.driverId = res.data.data.pkId;
          this.uploadLocation();
        } else {
          // 登录失败，清空token并修改登录状态
          Taro.setStorageSync("token", "");
          setGlobalData("isLogin", false);
        }
      });
    },
    // 司机上报自己的位置
    uploadLocation() {
      var that = this;
      Taro.getLocation({
        type: "wgs84",
        success(res) {
          that.latitude = res.latitude;
          that.longitude = res.longitude;
        },
      });
      Taro.request({
        url: baseUrl.platform + "travel/postion/heartReport",
        method: "GET",
        data: {
          driverId: this.driverId,
          lng: this.longitude,
          lat: this.latitude,
        },
      }).then((res) => {
        console.log("司机上报位置", res.data);
      });
    },
    // 查询接单情况
    getOrder() {
      // 接单状态
      Taro.request({
        url: baseUrl.platform + "travel/push/driver",
        data: {
          driverId: this.driverId,
        },
      }).then((res) => {
        console.log("轮询接单", res.data);
        if (res.data.code == 200) {
          clearInterval(this.timeId);
        }
        if (res.data.code == 200) {
          this.orderList.push(res.data.data);
        }
      });
    },
  },
};
</script>